package com.zhangdi.mgsync.core.rest.admin;

import com.zhangdi.mgsync.core.enums.Exchanges;
import com.zhangdi.mgsync.core.listener.SyncListener;
import com.zhangdi.mgsync.core.message.EmailUtil;
import com.zhangdi.mgsync.core.rabbit.AdminTemplate;
import com.zhangdi.mgsync.core.request.MultiListenerRequest;
import com.zhangdi.mgsync.core.request.SingleListenerRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2021.11.2 17:01
 */

@RestController
@RequestMapping("/mgsync/admin")
public class AdminController {
    Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Resource
    SyncListener listener;
    @Resource(name = "onlineAdminTemplate")
    AdminTemplate onlineAdminTemplate;
    @Resource(name = "tenderAdminTemplate")
    AdminTemplate tenderAdminTemplate;
    @Resource(name = "standardAdminTemplate")
    AdminTemplate standardAdminTemplate;

    private boolean create(String exchange, String queue) {
        if (Exchanges.mgsync.name().equals(exchange)) {
            return onlineAdminTemplate.create(exchange, queue);
        } else if (Exchanges.tender.name().equals(exchange)) {
            return tenderAdminTemplate.create(exchange, queue);
        } else if (Exchanges.standard.name().equals(exchange)) {
            return standardAdminTemplate.create(exchange, queue);
        }
        return false;
    }

    private boolean create(String exchange, Collection<String> queues) {
        if (Exchanges.mgsync.name().equals(exchange)) {
            return onlineAdminTemplate.create(exchange, queues);
        } else if (Exchanges.tender.name().equals(exchange)) {
            return tenderAdminTemplate.create(exchange, queues);
        } else if (Exchanges.standard.name().equals(exchange)) {
            return standardAdminTemplate.create(exchange, queues);
        }
        return false;
    }

    @PostMapping("start")
    public String start(@RequestBody MultiListenerRequest request) {
        if (request.getQueues().size() == 0) {
            logger.error("要监听的文档名字不存在");
            return "要监听的文档名字不存在";
        }

        try {
            create(request.getExchange_name(), request.getQueues());
        } catch (Exception e) {
            logger.error("创建失败交换器与队列失败", e);
            return "创建失败交换器与队列失败";
        }

        try {
            listener.startup(request.getExchange_name(), request.getQueues(), request.getTime());
        } catch (Exception e) {
            logger.error("启动增量监听失败", e);
            return "启动增量监听失败";
        }

        return "启动增量监听成功";
    }

    @GetMapping("/start/{ex}/{coll}/{sec}")
    public String start(@PathVariable("ex") String exchange, @PathVariable("coll") String coll, @PathVariable("sec") int seconds) {
        try {
            listener.startup(exchange, coll, seconds);
        } catch (Exception e) {
            logger.error("启动监听 {} 失败", coll, e);
            return "启动监听失败";
        }

        return "启动监听成功";
    }

    @PostMapping("/start/one")
    public String start(@RequestBody SingleListenerRequest request) {
        try {
            create(request.getExchange_name(), request.getQueue_name());
        } catch (Exception e) {
            logger.error("创建失败交换器与队列失败", e);
            return "创建失败交换器与队列失败";
        }

        try {
            listener.startup(request.getExchange_name(), request.getQueue_name(), request.getTime());
        } catch (Exception e) {
            logger.error("启动增量监听失败", e);
            return "启动增量监听失败";
        }

        return "启动监听成功";
    }

    @PostMapping("/createqueue/{exchange}/{queue}")
    public String createQueue(@PathVariable("exchange") String exchange, @PathVariable("queue") String queue) {
        create(exchange, queue);
        return "创建完成";
    }

    @GetMapping("shutdown")
    public String shutdown() {
        listener.shutdown();

        return "关闭线程任务";
    }

    @GetMapping("shutdown/{queue}")
    public String shutdown(@PathVariable("queue") String queue) {
        listener.shutdown(queue);
        return "关闭线程任务";
    }

    @GetMapping("restart")
    public String restart(String exchange, String queues) {
//        listener.shutdown();

        return "重启，暂未实现";
    }

    @GetMapping("checkkey/{key}")
    public String checkKey(@PathVariable("key") String key) {
        return listener.checkKey(key);
    }

    @GetMapping("cache/{key}/{val}")
    public String addCache(@PathVariable("key") String key, @PathVariable("val") String val) {
        return listener.addCache(key, val);
    }

    @Autowired
    EmailUtil emailUtil;

    @GetMapping("/mail/{ctx}")
    public String mailTest(@PathVariable("ctx") String ctx) {
        emailUtil.send("测试邮件", ctx);
        return "发了";
    }
}
